package cn.bsb.controller.backstage;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;

import cn.bsb.pojo.BackendUser;
import cn.bsb.pojo.Thematic;
import cn.bsb.service.backstage.backendUserService;
import cn.bsb.service.backstage.ArticleService;
import cn.bsb.service.backstage.ThematicService;
import cn.bsb.tools.Constants;

@Controller
@RequestMapping(value = "thematic")
public class thematicController {
	@Autowired
	private ArticleService article;
	@Autowired
	private ThematicService thematic;
	@Autowired
	private backendUserService backendUser;
	
	//����ר���б�
	@RequestMapping("thematicIndex")
	public String inThematicIndex(Model model,
			HttpSession session,HttpServletRequest request,
			@RequestParam(value="pageNum",required=false,defaultValue="1") int pageNum) {
		//ר���ҳ
		PageInfo<Thematic> page = thematic.getAllList(pageNum, 10);
		model.addAttribute("page", page);
		//����Ա�б�����ҳ����ʾ��ר��Ĵ����ߺ��޸���
		List<BackendUser> list = backendUser.getList();
		model.addAttribute("list", list);
		return "backstage/thematiclist";
	}
	
	//��ѯ�����ר���б�
	@RequestMapping("thematicSelect")
	public String inThematicSelect(@RequestParam("name") String name,
			Model model, HttpSession session, HttpServletRequest request) {
		//�鵽��ר���б�
		List<Thematic> listthematic = thematic.getList(name);
		model.addAttribute("listthematic", listthematic);
		//����Ա�б�����ҳ����ʾ��ר��Ĵ����ߺ��޸���
		List<BackendUser> list = backendUser.getList();
		model.addAttribute("list", list);
		return "backstage/thematiclist";
	}
	
	//�������ר����޸�ר��
	@RequestMapping("thematicInAdd")
	public String inthematicAdd(@RequestParam(value="id")Long id,
			Model model, HttpSession session, HttpServletRequest request) {
		if(id == 0) {
			return "backstage/thematicAdd";
		}else if(id != 0) {
			Thematic matic = thematic.getOne(id);
			model.addAttribute("matic", matic);
		}
		return "backstage/thematicAdd";
	}
	//���ר��
	@RequestMapping("thematicAdd")
	public String thematicAdd(@RequestParam("name") String name,
			Model model, HttpSession session, HttpServletRequest request) {
		BackendUser user = (BackendUser) session.getAttribute(Constants.admin_SESSION);
		int a = thematic.addThematic(name, user.getId());
		return "redirect:/thematic/thematicIndex";
	}
	//�޸�ר��
	@RequestMapping("thematicUpdate")
	public String thematicUpdate(@RequestParam("name") String name,
			@RequestParam("id")Long id,
			Model model, HttpSession session, HttpServletRequest request) {
		BackendUser user = (BackendUser) session.getAttribute(Constants.admin_SESSION);
		int a = thematic.updateThematic(id, name, user.getId());
		return "redirect:/thematic/thematicIndex";
	}
	//删除专题
	@RequestMapping("thematicDelete")
	public String thematicDelete(@RequestParam("id")Long id) {
		int a = thematic.deleteThematic(id);
		int i=article.updateThematicid(id);
		return "redirect:/thematic/thematicIndex";
	}
}
